﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ResourceLoadBalancer.WebServices.Contracts.Data;
using System.ServiceModel;

namespace ResourceLoadBalancer.WebServices.Clients.Helpers
{
    public class ResourceLoadBalancerServiceClientHelper
    {
        ResourceLoadBalancerServiceClient resourceServiceClient;
        
        public ResourceLoadBalancerServiceClientHelper()
        {
            resourceServiceClient = new ResourceLoadBalancerServiceClient();
            resourceServiceClient.Open();
        }
        
        ~ResourceLoadBalancerServiceClientHelper()
        {
            resourceServiceClient.Close();
        }

        public void AddResource(ResourceDataContract resource)
        {

                try
                {
                    resourceServiceClient.AddResource(resource);

                    
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                
        }

        public void AddResources(IList<ResourceDataContract> resources)
        {

            try
            {
                resourceServiceClient.AddResources(resources);


            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public ResourceURIDataContract AddResourceURI(ResourceURIDataContract resourceURI)
        {
            
                try
                {
                    resourceURI = resourceServiceClient.AddResourceURI(resourceURI);

                    return resourceURI;

                }
                catch (Exception ex)
                {
                    throw ex;
                }
        }

        public bool IsServiceAvailable()
        {
            bool serviceAvailable = false;
            try
            {
                serviceAvailable = resourceServiceClient.IsServiceAvailable();

            }
            catch (Exception)
            {
                Console.WriteLine("Service unavailable");
            }
            return serviceAvailable;
        }

        public void CleanResourceURIs(string hostName, string path)
        {

            try
            {
                resourceServiceClient.CleanResourceURIs(hostName, path);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void ConfigureQueuing(string hostName, string queueType, string queueName)
        {

            try
            {
                resourceServiceClient.ConfigureQueuing(hostName,queueType,queueName);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeconfigureQueuing(string hostName, string queueType, string queueName)
        {

            try
            {
                resourceServiceClient.DeconfigureQueuing(hostName, queueType, queueName);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void QueueFileSystemResourceURIs(string hostName, string queueName, IList<ResourceURIDataContract> resourceURIs)
        {

            try
            {
                resourceServiceClient.QueueFileSystemResourceURIs(hostName, queueName, resourceURIs);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string GetQueuedFileSystemResourceURI(string hostName, string queueName)
        {
            string queuedItem = "";
            try
            {
                queuedItem = resourceServiceClient.GetQueuedFileSystemResourceURI(hostName, queueName);

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return queuedItem;
        }

        public void QueueSharePointResourceURI(SharePointURIDataContract resourceURI)
        {

            try
            {
                resourceServiceClient.QueueSharePointResourceURI(resourceURI);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public SharePointURIDataContract GetQueuedSharePointResourceURI()
        {
            SharePointURIDataContract queuedItem = null;
            try
            {
                queuedItem = resourceServiceClient.GetQueuedSharePointResourceURI();

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return queuedItem;
        }


        public void AddResourceURIs(IList<ResourceURIDataContract> resourceURIs)
        {

            try
            {
                resourceServiceClient.AddResourceURIs(resourceURIs);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public ResourceURIDataContract GetResourceURI(ResourceURIDataContract resourceURI)
        {

            try
            {
                resourceURI = resourceServiceClient.GetResourceURI(resourceURI);

                return resourceURI;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public IList<ResourceURIDataContract> GetResourceURIs(string uri)
        {

            try
            {
                IList<ResourceURIDataContract> resourceURIs = resourceServiceClient.GetResourceURIs(uri);

                return resourceURIs;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
